home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / system-config-printer / troubleshoot / CheckUSBPermissions.py < prev    next >
Text File  |  2009-10-19  |  6KB  |  167 lines

  1. #!/usr/bin/env python
  2.  
  3. ## Printing troubleshooter
  4.  
  5. ## Copyright (C) 2008, 2009 Red Hat, Inc.
  6. ## Copyright (C) 2008, 2009 Tim Waugh <twaugh@redhat.com>
  7.  
  8. ## This program is free software; you can redistribute it and/or modify
  9. ## it under the terms of the GNU General Public License as published by
  10. ## the Free Software Foundation; either version 2 of the License, or
  11. ## (at your option) any later version.
  12.  
  13. ## This program is distributed in the hope that it will be useful,
  14. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. ## GNU General Public License for more details.
  17.  
  18. ## You should have received a copy of the GNU General Public License
  19. ## along with this program; if not, write to the Free Software
  20. ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. import glob
  23. import os
  24. import subprocess
  25. from timedops import TimedSubprocess
  26. import urllib
  27. from base import *
  28. class CheckUSBPermissions(Question):
  29.     def __init__ (self, troubleshooter):
  30.         Question.__init__ (self, troubleshooter, "Check USB permissions")
  31.         troubleshooter.new_page (gtk.Label (), self)
  32.  
  33.     def display (self):
  34.         self.answers = {}
  35.         answers = self.troubleshooter.answers
  36.         if answers['cups_queue_listed']:
  37.             if answers['is_cups_class']:
  38.                 return False
  39.  
  40.             cups_printer_dict = answers['cups_printer_dict']
  41.             device_uri = cups_printer_dict['device-uri']
  42.         elif answers.get ('cups_device_listed', False):
  43.             device_uri = answers['cups_device_uri']
  44.         else:
  45.             return False
  46.  
  47.         (scheme, rest) = urllib.splittype (device_uri)
  48.         if scheme not in ['hp', 'hpfax', 'usb', 'hal']:
  49.             return False
  50.  
  51.         LSUSB = "/sbin/lsusb"
  52.         if not os.access (LSUSB, os.X_OK):
  53.             return False
  54.  
  55.         GETFACL = "/usr/bin/getfacl"
  56.         if not os.access (GETFACL, os.X_OK):
  57.             return False
  58.  
  59.         # Run lsusb
  60.         parent = self.troubleshooter.get_window ()
  61.         try:
  62.             self.op = TimedSubprocess (parent=parent,
  63.                                        args="LC_ALL=C " + LSUSB + " -v",
  64.                                        shell=True,
  65.                                        stdin=file("/dev/null"),
  66.                                        stdout=subprocess.PIPE,
  67.                                        stderr=subprocess.PIPE)
  68.             (lsusb_stdout, lsusb_stderr, result) = self.op.run ()
  69.         except:
  70.             # Problem executing command.
  71.             return False
  72.  
  73.         # Now parse it.
  74.         dev_by_id = {}
  75.         this_dev = None
  76.         for line in lsusb_stdout:
  77.             if (this_dev != None and
  78.                 ((line.find ("bInterfaceClass") != -1 and
  79.                   line.find ("7 Printer") != -1) or
  80.                  (line.find ("bInterfaceSubClass") != -1 and
  81.                   line.find ("1 Printer") != -1))):
  82.                 mfr = dev_by_id.get (this_mfr_id, {})
  83.                 mdl = mfr.get (this_mdl_id, [])
  84.                 mdl.append (this_dev)
  85.                 mfr[this_mdl_id] = mdl
  86.                 dev_by_id[this_mfr_id] = mfr
  87.                 this_dev = None
  88.                 continue
  89.  
  90.             separators = [ ('Bus ', 3),
  91.                            (' Device ', 3),
  92.                            (': ID ', 4),
  93.                            (':', 4),
  94.                            (' ', -1)]
  95.             fields = []
  96.             i = 0
  97.             p = line
  98.             while i < len (separators):
  99.                 (sep, length) = separators[i]
  100.                 if not p.startswith (sep):
  101.                     break
  102.                 start = len (sep)
  103.                 if length == -1:
  104.                     end = len (p)
  105.                     fields.append (p[start:])
  106.                 else:
  107.                     end = start + length
  108.                     fields.append (p[start:end])
  109.  
  110.                 p = p[end:]
  111.                 i += 1
  112.  
  113.             if i < len (separators):
  114.                 continue
  115.  
  116.             if not scheme.startswith ('hp') and fields[2] != '03f0':
  117.                 # Skip non-HP printers if we know we're using HPLIP.
  118.                 continue
  119.  
  120.             this_dev = { 'bus': fields[0],
  121.                          'dev': fields[1],
  122.                          'name': fields[4],
  123.                          'full': line }
  124.             this_mfr_id = fields[2]
  125.             this_mdl_id = fields[3]
  126.  
  127.         infos = {}
  128.         paths = []
  129.         if not scheme.startswith ('hp'):
  130.             paths.extend (glob.glob ("/dev/usb/lp?"))
  131.         for mfr_id, mdls in dev_by_id.iteritems ():
  132.             for mdl_id, devs in mdls.iteritems ():
  133.                 for dev in devs:
  134.                     path = "/dev/bus/usb/%s/%s" % (dev['bus'], dev['dev'])
  135.                     paths.append (path)
  136.                     infos[path] = dev['full']
  137.  
  138.         perms = []
  139.         for path in paths:
  140.             try:
  141.                 self.op = TimedSubprocess (parent=parent,
  142.                                            args="LC_ALL=C %s %s" % (GETFACL,
  143.                                                                     path),
  144.                                            shell=True,
  145.                                            stdin=file("/dev/null"),
  146.                                            stdout=subprocess.PIPE,
  147.                                            stderr=subprocess.PIPE)
  148.                 (getfacl_stdout, getfacl_stderr, result) = self.op.run ()
  149.                 output = filter (lambda x: len (x) > 0, getfacl_stdout)
  150.             except:
  151.                 # Problem executing command.
  152.                 output = []
  153.  
  154.             info = infos.get (path, path)
  155.             perms.append ((info, output))
  156.  
  157.         self.answers['getfacl_output'] = perms
  158.  
  159.         # Don't actually display anything, just collect information.
  160.         return False
  161.  
  162.     def collect_answer (self):
  163.         return self.answers
  164.  
  165.     def cancel_operation (self):
  166.         self.op.cancel ()
  167.